Skip to content

Persist the segbuild byte counter, or a restarted node never cuts - #406

Merged
SergioDemianLerner merged 1 commit into
mainfrom
segbuild/durable-counter
Nov 22, 2026
Merged

SergioDemianLerner merged 1 commit into
mainfrom
segbuild/durable-counter

Conversation

@SDL-A1

@SDL-A1 SDL-A1 commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

The design argued the segbuild byte counter need not be durable: it resets on restart, the segment in progress is cut late, and sizes are advisory anyway. That reasoning is sound for one late cut and breaks at the time scale the mainnet node actually runs at.

Measured from this node's own epoch rotations — which use the same 1,024 MB threshold, so a completed epoch's block span is a segment's:

epoch blocks for one full 1,024 MB at 30 s/block
2 (#9,342,320 → #9,367,401) 25,081 8.7 days
3 (#9,367,401 → #9,417,643) 50,242 17.4 days

So a node restarted weekly returned to zero every time and would never reach the target and never cut a boundary at all. Not "a segment cut late": no segments, ever, with a log that looks healthy and a feature that extends nothing. That invisibility is what makes it worth fixing rather than documenting.

What changed

COUNTER is written by the poll that cuts boundaries, so the hot path is still one relaxed atomic add — making it durable deliberately did not move work into the block being executed. The write is skipped when the value has not moved, so an idle node writes nothing.

The asymmetry with the boundary log is deliberate. COUNTER is rewritten in place and not fsynced; BOUNDARIES is appended and fsynced. Losing the last poll's accounting to a power cut delays a cut, which is what "advisory" genuinely covers. Losing a boundary merges two segments into one that may not fit a consumer's memory, which is the property the sizing exists to protect. A torn or unparseable counter therefore opens as zero rather than refusing: starting the segment over costs one late cut, and refusing would stop the set being extended over a number that only decides where to cut.

A cut clears the persisted value, and only after the boundary is appended. Persisting the reset first would discard a segment's worth of accounting if the append then failed, to save nothing; leaving it means a restart resumes the count instead.

Verification

Storage 17 tests, three of them new:

  • the counter resumes across a restart, and an unmoved counter writes nothing
  • a cut clears the persisted value, so a restart straight after one does not cut again immediately
  • a torn, empty or unparseable COUNTER opens as zero

CLI bin 164, clippy clean. docs/segbuild-continuous-design.md §4.1 and the §5 failure table are corrected — the old "node restarts | counter resets, so one segment is cut late. Harmless: sizes are advisory" row was the claim that turned out to be wrong.

Note for deployment

The node currently running has the pre-fix binary, so its counter still resets on restart. Nothing breaks in the meantime — it cut its catch-up boundary from the executed head at startup and built 909-9420001-9429888 — but the next size-driven boundary is 9–18 days out and every restart before then starts that clock over.

🤖 Generated with Claude Code

The original design argued the counter need not be durable: it resets, the
segment in progress is cut late, and sizes are advisory anyway. That reasoning
is sound for *one* late cut and breaks at the time scale the mainnet node
actually runs at.

Measured from this node's own epoch rotations, which use the same 1,024 MB
threshold, a gigabyte of written state is 25,000 to 50,000 blocks -- nine to
eighteen days:

    epoch 2  9,342,320 -> 9,367,401   25,081 blocks    8.7 days
    epoch 3  9,367,401 -> 9,417,643   50,242 blocks   17.4 days

So a node restarted weekly returned to zero every time and would **never reach
the target and never cut a boundary at all**. Not "a segment cut late": no
segments, ever, with a log that looks healthy and a feature that extends
nothing. The failure is invisible, which is what makes it worth fixing rather
than documenting.

`COUNTER` is written by the poll that cuts boundaries, so the hot path is still
one relaxed atomic add -- making it durable deliberately did not move work into
the block being executed. The write is skipped when the value has not moved, so
an idle node writes nothing.

The asymmetry with the boundary log is deliberate and the module note explains
it: `COUNTER` is rewritten in place and not fsynced, while `BOUNDARIES` is
appended and fsynced. Losing the last poll's accounting to a power cut delays a
cut, which is what "advisory" genuinely covers; losing a boundary merges two
segments into one that may not fit a consumer's memory, which is the property
the sizing exists to protect. A torn or unparseable counter therefore opens as
zero rather than refusing: starting the segment over costs one late cut, and
refusing would stop the set being extended over a number that only decides
where to cut.

A cut clears the persisted value too, and only after the boundary has been
appended. Persisting the reset first would throw away a segment's worth of
accounting if the append then failed, to save nothing; leaving it means a
restart resumes the count instead.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@SergioDemianLerner
SergioDemianLerner merged commit 301c231 into main Nov 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants